using System;
using System.Collections.Generic;
using System.Text;
using Index.TemporaryFile.Sorting.Merge;

namespace Index.TemporaryFile
{
    public class SortedFileEntryReader
    {

        private InputQueue reader = null;

        public SortedFileEntryReader(TermEntryFile file, long memory)
        {
            reader = new InputQueue(file, (int) (memory / 12), 0, file.TermEntryCount - 1);
        }

        public TermEntry[] GetNextEntriesWithSameTerm()
        {
            List<TermEntry> entries = new List<TermEntry>();
            TermEntry last = null;
            TermEntry current = null;

            while ((reader.Count > 0) && (current = reader.Peek()) != null)
            {

                if (last == null)
                {
                    last = current;
                    entries.Add(reader.Dequeue());
                }
                else
                {
                    if (last.TermID == current.TermID)
                    {
                        entries.Add(reader.Dequeue());
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (entries.Count == 0)
            {
                return null;
            }
            else
            {
                return entries.ToArray();
            }

        }
    }
}
